package gest;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Medicamento_Impl extends UnicastRemoteObject implements Medicamento {

	public static Connection conn;
	private int idMedi;
	private String nome;
	
	public Medicamento_Impl(String nome) throws RemoteException {
		this.nome = nome;
		Statement st;
		ResultSet rs;
		if (conn == null)
			conn = DBConnection.get_conn();
		try {
			st = conn.createStatement();
			String query = "select idMedi from Medicamentos where nome='"+this.nome+"'";
			rs = st.executeQuery(query);
			if (rs.first()) {
				this.idMedi = rs.getInt(1);	
			} else {
				st.execute("insert into Medicamentos (nome) values ('"
						+ nome + "')", st.RETURN_GENERATED_KEYS);
				rs = st.getGeneratedKeys();
				rs.first();
				this.idMedi = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public String get_nome() throws RemoteException {
		return this.nome;
	}

	public int get_idMedi() throws RemoteException {
		return this.idMedi;
	}

}
